package org.springblade.gold.controller;

import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.gold.crawler.GoldDataCrawler;
import org.springblade.gold.pojo.entity.InfoEntity;
import org.springblade.gold.service.IInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/gold")
public class GoldDataController {

    @Autowired
    private IInfoService infoService;

    /**
     * 获取黄金价格的接口
     * @return 黄金价格字符串
     */
    @GetMapping("/getRealPrice")
    public R getRealPrice() {
        List<List<String>> goldPrices = GoldDataCrawler.fetchRealGoldPrice();

        // 将获取到的数组逐条插入数据库
//		Date importTime = DateUtil.now();
//        goldPrices.forEach(priceData -> {
//            if (priceData.size() > 0) {
//                InfoEntity infoEntity = new InfoEntity();
//                infoEntity.setName(priceData.get(0));
//                infoEntity.setPrice(priceData.get(1));
//                infoEntity.setChange(priceData.get(2));
//                infoEntity.setChangeRate(priceData.get(3));
//                infoEntity.setHighPrice(Double.valueOf(priceData.get(4)));
//                infoEntity.setLowPrice(Double.valueOf(priceData.get(5)));
//                infoEntity.setLastPrice(Double.valueOf(priceData.get(6)));
//				infoEntity.setImportTime(importTime);
//
//                String timePart = priceData.get(7);
//                String todayDate = java.time.LocalDate.now().toString();
//                String combinedDateTime = todayDate + " " + timePart;
//				Date date = DateUtil.parse(combinedDateTime, "yyyy-MM-dd HH:mm:ss");
//                infoEntity.setDataTime(date);
//
//                // 调用 InfoService 插入数据
////                infoService.save(infoEntity);
//            }
//        });

		return R.data(goldPrices);
    }
	/**
	 * 获取黄金价格的接口
	 * @return 黄金价格字符串
	 */
	@GetMapping("/getShPrice")
	public R getShPrice() {
		List<List<String>> goldPrices = GoldDataCrawler.fetchGoldPrice();

		// 将获取到的数组逐条插入数据库
		Date importTime = DateUtil.now();
		goldPrices.forEach(priceData -> {
			if (priceData.size() > 0) {
				InfoEntity infoEntity = new InfoEntity();
				infoEntity.setName(priceData.get(0));
				infoEntity.setPrice(priceData.get(1));
				infoEntity.setChange(priceData.get(2));
				infoEntity.setChangeRate(priceData.get(3));
				infoEntity.setHighPrice(Double.valueOf(priceData.get(4)));
				infoEntity.setLowPrice(Double.valueOf(priceData.get(5)));
				infoEntity.setLastPrice(Double.valueOf(priceData.get(6)));
				infoEntity.setImportTime(importTime);

				String timePart = priceData.get(7);
				String todayDate = java.time.LocalDate.now().toString();
				String combinedDateTime = todayDate + " " + timePart;
				Date date = DateUtil.parse(combinedDateTime, "yyyy-MM-dd HH:mm:ss");
				infoEntity.setDataTime(date);

				// 调用 InfoService 插入数据
//                infoService.save(infoEntity);
			}
		});

		return R.data(goldPrices);
	}
	/**
	 * 获取黄金价格的接口
	 * @return 黄金价格字符串
	 */
	@GetMapping("/getInterPrice")
	public R getInterPrice() {
		List<List<String>> goldPrices = GoldDataCrawler.fetchInterGoldPrice();

		// 将获取到的数组逐条插入数据库
//		Date importTime = DateUtil.now();
//		goldPrices.forEach(priceData -> {
//			if (priceData.size() > 0) {
//				InfoEntity infoEntity = new InfoEntity();
//				infoEntity.setName(priceData.get(0));
//				infoEntity.setPrice(priceData.get(1));
//				infoEntity.setChange(priceData.get(2));
//				infoEntity.setChangeRate(priceData.get(3));
//				infoEntity.setHighPrice(Double.valueOf(priceData.get(4)));
//				infoEntity.setLowPrice(Double.valueOf(priceData.get(5)));
//				infoEntity.setLastPrice(Double.valueOf(priceData.get(6)));
//				infoEntity.setImportTime(importTime);
//
//				String timePart = priceData.get(7);
//				String todayDate = java.time.LocalDate.now().toString();
//				String combinedDateTime = todayDate + " " + timePart;
//				Date date = DateUtil.parse(combinedDateTime, "yyyy-MM-dd HH:mm:ss");
//				infoEntity.setDataTime(date);
//
//				// 调用 InfoService 插入数据
////                infoService.save(infoEntity);
//			}
//		});

		return R.data(goldPrices);
	}
}
